package org.custom.spring.boot.transaction.controller.async;

import org.custom.spring.boot.transaction.service.async.AsyncTransactionService;
import org.custom.spring.boot.useless.Result;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * 异步事务服务网络接口
 *
 * @author LiKun
 * @date 2021/9/17 8:49
 */
@RestController
@RequestMapping("/async")
public class AsyncTransactionController {
    @Resource
    private AsyncTransactionService asyncTransactionService;

    @Result("两个事务，A回滚")
    @GetMapping("/{data1}/{data2}")
    @Description("A(抛出异常)异步调用B")
    public int validAsyncTransaction(@PathVariable("data1") String data1, @PathVariable("data2") String data2) {
        return asyncTransactionService.transaction(data1, data2);
    }

    @Result("一个事务，A回滚")
    @Description("A(抛出异常)异步调用b")
    @GetMapping("/without/{data1}/{data2}")
    public int validAsyncWithoutTransaction(@PathVariable("data1") String data1, @PathVariable("data2") String data2) throws ExecutionException, InterruptedException {
        return asyncTransactionService.validAsyncWithoutTransaction(data1, data2);
    }

    @Result("两个事务，B回滚")
    @Description("A异步调用B(抛出异常)")
    @GetMapping("/internal/{data1}/{data2}")
    public int validAsyncTransactionInternal(@PathVariable("data1") String data1, @PathVariable("data2") String data2) throws ExecutionException, InterruptedException {
        return asyncTransactionService.validAsyncTransactionInternal(data1, data2);
    }

    @Result("一个事务，B回滚")
    @Description("a异步调用B(抛出异常)")
    @GetMapping("/internal/without/{data1}/{data2}")
    public int validAsyncWithoutTransactionInternal(@PathVariable("data1") String data1, @PathVariable("data2") String data2) throws ExecutionException, InterruptedException {
        return asyncTransactionService.validAsyncWithoutTransactionInternal(data1, data2);
    }

    @Result("一个事务，B回滚")
    @Description("a异步调用B(B抛出异常，a捕获B抛出的异常)")
    @GetMapping("/internal/without/catch/{data1}")
    public int validAsyncWithoutTransactionInternalWithCatch(@PathVariable("data1") String data1) {
        return asyncTransactionService.valid_a_Async_B_And_a_Catch_B_Exception(data1);
    }
}
